<?php

namespace App\Http\Controllers\Admin;
use App\Http\Model\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    public function Index(){
        return view('admin.index');
    }
    public function Info(){
        return view('admin.info');
    }

    public function Pass()
    {
        if ($input = Input::except('_token')) {
            $roules = [
                'password' => 'required|between:6,20|confirmed',
            ];
            $message = [
                'password.required' => '新密码不能为空!',
                'password.between' => '新密码长度必须在6-20字符之间!',
                'password.confirmed' => '新密码和确认密码不一致!',
            ];
            $validator = Validator::make($input, $roules, $message);
            if ($validator->passes()) {
                $user = Admin::first();
                $_password = Crypt::decrypt($user->password);
                if ($input['password_o'] == $_password) {
                    $user->password = Crypt::encrypt($input['password']);
                    $user->update();
                    return back()->with('errors', '修改成功!');
                } else {
                    return back()->with('errors', '原密码错误!');
                }
            } else {
                return back()->withErrors($validator)->withInput();
            }
        } else {
            return view('admin.pass');
        }
    }


}
